const buffer = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x48, 0x65, 0x6c, 0x6c, 0x6f]);

// 读取单个字节为整数
const int8 = buffer.readUInt8(0);
console.log(int8); // 输出: 1 (number)

// 读取单个字节为新的 Buffer 对象
const byteBuffer = buffer.slice(0, 1);
console.log(byteBuffer); // 输出: <Buffer 01> (Buffer)

// 读取多个字节为整数
const int16 = buffer.readUInt16BE(0);
console.log(int16); // 输出: 258 (number)

const int32 = buffer.readUInt32BE(0);
console.log(int32); // 输出: 16909060 (number)

// 读取 64 位整数
const buffer64 = Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02]);
const int64 = buffer64.readBigUInt64BE(0);
console.log(int64); // 输出: 258n (BigInt)

// 读取部分数据为字符串
const str = buffer.toString('utf8', 4, 9);
console.log(str); // 输出: Hello (string)

// 读取浮点数
const floatBuffer = Buffer.from([0x40, 0x49, 0x0f, 0xdb, 0x40, 0x00, 0x00, 0x00]);
const floatVal = floatBuffer.readFloatBE(0);
console.log(floatVal); // 输出: 3.1415927410125732 (number)

const doubleVal = floatBuffer.readDoubleBE(0);
console.log(doubleVal); // 输出: 3.141592653589793 (number)

// 读取部分数据为 Buffer
const subBuffer = buffer.slice(1, 3);
console.log(subBuffer); // 输出: <Buffer 02 03> (Buffer)
